package com.liser.southernstar.collection.ui.fragment;

import android.view.View;
import android.widget.LinearLayout;

import androidx.coordinatorlayout.widget.CoordinatorLayout;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.android.material.bottomsheet.BottomSheetBehavior;
import com.kunminx.linkage.LinkageRecyclerView;
import com.liser.southernstar.R;
import com.liser.southernstar.framework.ui.BaseFragment;
import com.liser.southernstar.model.mapper.ConversionFactory;
import com.liser.southernstar.collection.model.dto.GoodsFamilyDTO;
import com.liser.southernstar.collection.model.dto.GoodsItemDTO;
import com.liser.southernstar.collection.model.vo.OrderItemVO;
import com.liser.southernstar.collection.model.vo.OrderTypeVO;
import com.liser.southernstar.net.Network;
import com.liser.southernstar.net.observer.NetworkSubs;
import com.liser.southernstar.collection.ui.adapter.OrderCarAdapter;
import com.liser.southernstar.neleme.utils.ViewUtils;
import com.liser.southernstar.neleme.views.AddGoodsWidget;
import com.liser.southernstar.neleme.views.ShopCarView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * @author LISER
 */
public class Order3Fragment extends BaseFragment {

    @BindView(R.id.menu_layout)
    CoordinatorLayout mMenuCoordinatorLayout;

    @BindView(R.id.linkage)
    LinkageRecyclerView linkageRecyclerView;

    @BindView(R.id.car_container)
    LinearLayout carContainer;

    @BindView(R.id.car_mainfl)
    ShopCarView shopCarView;

    @BindView(R.id.car_recyclerview)
    RecyclerView carRecView;

    @BindView(R.id.blackview)
    View blackView;

    private BottomSheetBehavior bottomSheetBehavior;

    private OrderCarAdapter orderCarAdapter;

    private AddGoodsWidget.OnAddClick onAddClick = new AddGoodsWidget.OnAddClick() {

        @Override
        public void onAddClick(View view, OrderItemVO orderItemVO) {
//            refreshCar(orderItemVO);
            ViewUtils.addTvAnim(view, shopCarView.carLoc, getContext(), mMenuCoordinatorLayout);
        }

        @Override
        public void onSubClick(OrderItemVO orderItemVO) {
//            refreshCar(orderItemVO);
        }
    };

    @Override
    protected int onLayout() {
        return R.layout.fragment_order;
    }

    @Override
    protected void onView() {
        initOrderCar();
        initNet();
    }

    private void initOrderCar() {
        bottomSheetBehavior = BottomSheetBehavior.from(carContainer);
        shopCarView.setBehavior(bottomSheetBehavior, blackView);
        carRecView.setLayoutManager(new LinearLayoutManager(getContext()));
        if (carRecView.getItemAnimator() != null) {
            ((DefaultItemAnimator) carRecView.getItemAnimator()).setSupportsChangeAnimations(false);
        }
        orderCarAdapter = new OrderCarAdapter(new ArrayList<>(), onAddClick);
        orderCarAdapter.bindToRecyclerView(carRecView);
    }

    private void initNet() {
        Network.api().getDataByBiz("412929567079170008")
                .compose(bindToLifecycle())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new NetworkSubs<List<GoodsFamilyDTO>>() {
                    @Override
                    public void onSuccess(List<GoodsFamilyDTO> data) {
                        List<OrderTypeVO> orderTypeVOList = new ArrayList<>();
                        List<OrderItemVO> orderItemVOList = new ArrayList<>();
                        for (GoodsFamilyDTO goodsFamilyDTO : data) {
                            OrderTypeVO orderTypeVO = ConversionFactory.orderTypeVO(goodsFamilyDTO);
                            orderTypeVOList.add(orderTypeVO);
                            List<GoodsItemDTO> goodsItemDTOList = goodsFamilyDTO.getGoodsList();
                            for (GoodsItemDTO goodsItemDTO : goodsItemDTOList) {
                                OrderItemVO orderItemVO = ConversionFactory.orderItemVO(goodsItemDTO);
                                orderItemVOList.add(orderItemVO);
                            }
                        }
                    }
                });
    }

    @OnClick(R.id.tv_clear_car)
    public void onClearCarClick() {
//        ViewUtils.showClearCar(getContext(), (dialog, which) -> clearCar());
    }

    /*private void refreshCar(OrderItemVO orderItemVO) {
        HashMap<String, Long> typeSelect = new HashMap<>();//更新左侧类别badge用
        BigDecimal amount = new BigDecimal(0.0);
        int total = 0;
        boolean hasFood = false;
        if (bottomSheetBehavior.getState() == BottomSheetBehavior.STATE_EXPANDED) {
            orderBodyWidget.orderItemAdapter.notifyDataSetChanged();
        }

        List<OrderItemVO> orderItemVOList = orderCarAdapter.getData();
        int p = -1;
        for (int i = 0; i < orderItemVOList.size(); i++) {
            OrderItemVO fb = orderItemVOList.get(i);
            if (fb.getId().equals(orderItemVO.getId())) {
                fb = orderItemVO;
                hasFood = true;
                if (orderItemVO.getSelectCount() == 0) {
                    p = i;
                } else {
                    orderCarAdapter.setData(i, orderItemVO);
                }
            }
            total += fb.getSelectCount();
            if (typeSelect.containsKey(fb.getId())) {
                Long afterCount = typeSelect.get(orderItemVO.getName());
                typeSelect.put(fb.getName(), afterCount == null ? 0 : afterCount + fb.getSelectCount());
            } else {
                typeSelect.put(fb.getName(), fb.getSelectCount());
            }
            amount = amount.add(fb.getPrice().multiply(BigDecimal.valueOf(fb.getSelectCount())));
        }

        if (p >= 0) {
            orderCarAdapter.remove(p);
        } else if (!hasFood && orderItemVO.getSelectCount() > 0) {
            orderCarAdapter.addData(orderItemVO);
            if (typeSelect.containsKey(orderItemVO.getName())) {
                Long afterCount = typeSelect.get(orderItemVO.getName());
                typeSelect.put(orderItemVO.getName(), afterCount == null ? 0 : afterCount + orderItemVO.getSelectCount());
            } else {
                typeSelect.put(orderItemVO.getName(), orderItemVO.getSelectCount());
            }
            amount = amount.add(orderItemVO.getPrice().multiply(BigDecimal.valueOf(orderItemVO.getSelectCount())));
            total += orderItemVO.getSelectCount();
        }
        shopCarView.showBadge(total);
        orderBodyWidget.orderTypeAdapter.updateBadge(typeSelect);
        shopCarView.updateAmount(amount);
    }

    private void clearCar() {
        List<OrderItemVO> orderItemVOList = orderCarAdapter.getData();
        for (OrderItemVO orderItemVO : orderItemVOList) {
            orderItemVO.setSelectCount(0);
        }
        orderCarAdapter.setNewData(new ArrayList<>());
        orderBodyWidget.orderItemAdapter.notifyDataSetChanged();
        shopCarView.showBadge(0);
        orderBodyWidget.orderTypeAdapter.updateBadge(new HashMap<>());
        shopCarView.updateAmount(new BigDecimal(0.0));
    }*/
}
